 /***************************************************************************
 *   Copyright (C) 2010 by ZhangShine , All Rights Reserved                                  
 *   zhangshine0125@gmail.com                                              
 *                                                                         
 *   This program is free software; you can redistribute it and/or modify  
 *   it under the terms of the GNU General Public License as published by  
 *   the Free Software Foundation; either version 2 of the License, or     
 *   (at your option) any later version.                                   
 *                                                                         
 *   This program is distributed in the hope that it will be useful,       
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 *   GNU General Public License for more details.                          
 *                                                                         
 *   You should have received a copy of the GNU General Public License     
 *   along with this program; if not, write to the                         
 *   Free Software Foundation, Inc.,                                       
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             
 ***************************************************************************/

#include "notifer.h"
#include "ui_notifer.h"
#include "QLisaGlobal.h"
#include <QtCore>
#include <QtGui>

Notifer::Notifer(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Notifer)
{
    ui->setupUi(this);
    ui->frame_MainFrame->installEventFilter(this);
    ui->frame_MainFrame->setMouseTracking(true);
    ui->textEdit_Content->installEventFilter(this);
    ui->textEdit_Content->setMouseTracking(true);
//    ui->frame_MainFrame->setMouseTracking(true);
//    ui->textEdit_Content->setAttribute(Qt::WA_NoMousePropagation);
//    ui->pushButton_Exit->setAttribute(Qt::WA_NoMousePropagation);
//    ui->pushButton_Next->setAttribute(Qt::WA_NoMousePropagation);
//    ui->pushButton_Pre->setAttribute(Qt::WA_NoMousePropagation);
//    ui->layoutWidget->setAttribute(Qt::WA_NoMousePropagation);
    this->currentMessageNumber = 0;
    timer = new QTimer(this);
    http = new Http(this);
    this->setWindowFlags(Qt::FramelessWindowHint|Qt::Tool|Qt::WindowStaysOnTopHint);// frameless
    /* hide background */
    setAttribute(Qt::WA_TranslucentBackground, true);
    QPalette pal = palette();
    pal.setColor(QPalette::Background, QColor( 0, 0, 0, 0));
    setPalette(pal);
    
    this->hide();
    this->timePassby = 0.0;
    
    QObject::connect(ui->pushButton_Pre, SIGNAL(clicked()), this, SLOT(PreButtonClicked()));
    QObject::connect(ui->pushButton_Next, SIGNAL(clicked()), this, SLOT(NextButtonClicked()));
    QObject::connect(ui->pushButton_Exit, SIGNAL(clicked()), this, SLOT(ExitButtonClicked()));
    QObject::connect(ui->textEdit_Content->document(), SIGNAL(contentsChanged()), this, SLOT(adjustNotifierSize()));
}

Notifer::~Notifer()
{
    qDebug("notifer deleting!");
    delete ui;
}

void Notifer::setNotiferContentAndShow(QList<QLisa::NotifyObject> messageList)
{
    if(messageList.isEmpty())
        this->close();
    this->mList = messageList;
//    for(int i=0; i<messageList.length(); i+)
//    {
//        QLisa::NotifyObject obj = messageList.at(i);
//        QUrl url(obj.imageUrlPath);
//        QNetworkRequest request(url);
//        QPixmap pix = http->getImageFromUrl(request);
//        urlPixmapMap.insert(obj.imageUrlPath, pix);
//    }
    if(mList.length()==1)
    {
        ui->pushButton_Pre->setEnabled(false);
        ui->pushButton_Next->setEnabled(false);
        ui->label_Progress->setText("1/1");
    }
    else {
        ui->pushButton_Pre->setEnabled(false);;
        ui->label_Progress->setText("1/"+QString::number(mList.length()));
        qDebug("mlist length is %d", mList.length());
    }
    currentMessageNumber=0;
    setText();
    showIt();
}

void Notifer::setNotiferContentAndShow(QString msg)
{
    ui->pushButton_Pre->setEnabled(false);
    ui->pushButton_Next->setEnabled(false);
    ui->label_Progress->setText("1/1");
    setText(msg);
    showIt();
}

void Notifer::showIt()
{
    QDesktopWidget *desktop = QApplication::desktop();
    QRect rect = desktop->screenGeometry(this);
    this->setGeometry( rect.width()-width(), rect.height()-height()-40, width(), height());
    this->show();
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeOutDeal()));
    timer->start(1000);//1s
}

void Notifer::timeOutDeal()
{
    timePassby++;
    if(timePassby>10)
        fadeOut();
}

void Notifer::fadeOut()
{
    if(windowOpacity()<0.1)
        this->close();
    else
    {
        setWindowOpacity(1.0-(timePassby-10)*0.2);
    }
}

bool Notifer::eventFilter(QObject *target, QEvent *event)
{
    if(target==ui->frame_MainFrame)
    {
        switch(event->type())
        {
        case QEvent::MouseMove:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            return true;
            break;
        default:
            break;
        }
    }
    if(target==ui->textEdit_Content)
    {
        switch(event->type())
        {
        case QEvent::Enter:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            timer->stop();
            return true;
            break;
        case QEvent::Leave:
            timePassby = 0.0;
            setWindowOpacity(1.0);
            timer->start();
            return true;
            break;
        default:
            break;
        }
    }
    
    return false;
}

void Notifer::PreButtonClicked()
{
    currentMessageNumber--;
    if(currentMessageNumber<=0)
    {
        currentMessageNumber = 0;
        ui->pushButton_Pre->setEnabled(false);
    }
    ui->pushButton_Next->setEnabled(true);
    ui->label_Progress->setText(QString::number(currentMessageNumber+1)+"/"+QString::number(mList.length()));
    setText();
}

void Notifer::NextButtonClicked()
{
    currentMessageNumber++;
    if(currentMessageNumber>=(mList.length()-1))
    {
        currentMessageNumber = mList.length()-1;
        ui->pushButton_Next->setEnabled(false);
    }
    ui->pushButton_Pre->setEnabled(true);
    ui->label_Progress->setText(QString::number(currentMessageNumber+1)+"/"+QString::number(mList.length()));
    setText();
}

void Notifer::ExitButtonClicked()
{
    this->close();
}

void Notifer::setText()
{
    if(currentMessageNumber>=0&&currentMessageNumber<mList.length())
    {
        ui->textEdit_Content->setText(mList.at(currentMessageNumber).msg);
        ui->textEdit_Content->adjustSize();
    }
}

void Notifer::setText(QString msg)
{
        ui->textEdit_Content->setText(msg);
        ui->textEdit_Content->adjustSize();
}

void Notifer::resizeEvent(QResizeEvent *event)
{
    int extraHeight = event->size().height() - height();
    ui->frame_MainFrame->setFixedHeight(ui->frame_MainFrame->height()+extraHeight);
    ui->textEdit_Content->setFixedHeight(ui->textEdit_Content->height()+extraHeight);
}

void Notifer::adjustNotifierSize()
{
//    qDebug("height is %d", ui->textEdit_Content->contentsRect().height());
//    int extraHeight = ui->textEdit_Content->document()->documentLayout()->documentSize().height()  - 
//                      ui->textEdit_Content->height();
//    qDebug("text is "+ui->textEdit_Content->document()->toPlainText().toAscii());
//    qDebug("height is %d", extraHeight);
//    if(extraHeight>0)
//    {
//        resize(width(), height()+extraHeight);
//    }
}
